//
//  RegisterNewPhoneCodeViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/18.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "RegisterNewPhoneCodeViewController.h"
#import "VerfityCodeViewController.h"
#import "StatementViewController.h"
@interface RegisterNewPhoneCodeViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *textField;
@end

@implementation RegisterNewPhoneCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createRegisterPhoneNumberUI];
    // Do any additional setup after loading the view.
}
-(void)createRegisterPhoneNumberUI{
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setBackgroundImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    [backButton addTarget:self action:@selector(backForePage) forControlEvents:UIControlEventTouchUpInside];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(30));
        make.left.equalTo(self.view.mas_left).offset(10);
        make.width.mas_equalTo(scaleLeftAndRight(23));
        make.height.mas_equalTo(scaleTopAndBottom(23));
    }];
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backButton.mas_right).offset(10);
        make.width.mas_equalTo(scaleLeftAndRight(2));
        make.height.mas_equalTo(scaleTopAndBottom(25));
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(30));
    }];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = self.headerTitile;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view1.mas_bottom).offset(scaleTopAndBottom(30));
        make.left.equalTo(self.view.mas_left);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    UILabel *phoneTileLabel = [[UILabel alloc] init];
    phoneTileLabel.text = @"+86";
    [self.view addSubview:phoneTileLabel];
    [phoneTileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(30));
        make.width.mas_equalTo(scaleLeftAndRight(40));
        make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(50));
        make.height.mas_equalTo(scaleTopAndBottom(30));
    }];
    self.textField = [[UITextField alloc] init];
    self.textField.placeholder = @"请输入您的手机号";
    self.textField.delegate = self;
    [self.view addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneTileLabel.mas_right).offset(scaleLeftAndRight(20));
        make.height.mas_equalTo(scaleTopAndBottom(30));
        make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(50));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-30));
    }];
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textField.mas_bottom).offset(10);
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(30));
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(scaleTopAndBottom(1));
    }];
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.backgroundColor = ZColorRGB(244.0, 190.0, 65.0);
    [self.view addSubview:nextBtn];
    nextBtn.layer.cornerRadius = scaleTopAndBottom(25);
    nextBtn.layer.masksToBounds = YES;
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(30));
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(view2.mas_bottom).offset(scaleTopAndBottom(40));
        make.height.mas_equalTo(scaleTopAndBottom(50));
    }];
    if (_isRegister) {
        UILabel *introduceLabel = [[UILabel alloc] init];
        introduceLabel.text = @"注册成功即表示你同意";
        introduceLabel.font = [UIFont systemFontOfSize:15.0];
        introduceLabel.textAlignment = NSTextAlignmentRight;
        [self.view addSubview:introduceLabel];
        [introduceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(nextBtn.mas_bottom).offset(scaleTopAndBottom(10));
            make.left.equalTo(self.view.mas_left);
            make.height.mas_equalTo(scaleTopAndBottom(20));
            make.width.mas_equalTo(scaleLeftAndRight(210));
        }];
        UIButton *protocalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        protocalBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        protocalBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [protocalBtn setTitle:@"《途吉用户协议》" forState:UIControlStateNormal];
        [protocalBtn setTitleColor:ZColorRGB(244.0, 190.0, 65.0) forState:UIControlStateNormal];
        [protocalBtn addTarget:self action:@selector(goToStatemnetVc) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:protocalBtn];
        [protocalBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(nextBtn.mas_bottom).offset(scaleTopAndBottom(10));
            make.left.equalTo(introduceLabel.mas_right);
            make.height.mas_equalTo(scaleTopAndBottom(20));
            make.right.equalTo(self.view.mas_right);
        }];

    }
}
#pragma mark -获取验证码
-(void)getVerifyCode{
    if ([self isMobileNumber:self.textField.text]) {

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSString *url = ServerIp@"/customerApp/getSmsMail";
        NSDictionary *dict = @{@"phoneNumber":self.textField.text,@"type":self.type};
        [manager POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [[NSUserDefaults standardUserDefaults] setObject:self.textField.text forKey:@"phoneNumber"];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            if ([dict[@"result"] integerValue] == 0) {
                VerfityCodeViewController *vc = [[VerfityCodeViewController alloc] init];
                if (_isRegister) {
                   vc.backStr = @"注册";
                }else{
                    vc.backStr = @"找回密码";
                }
                vc.type = self.type;
                vc.phoneNumber = self.textField.text;
                [[NSUserDefaults standardUserDefaults] setObject:self.textField.text forKey:@"phoneNumber"];
                [self presentViewController:vc animated:YES completion:^{
                }];
            }else{
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:dict[@"msg"] message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }else{
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请确认您的手机号是否正确" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alertView show];
                }

}
#pragma mark -返回上一界面
-(void)backForePage{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma makr-
-(void)goToStatemnetVc{
    StatementViewController *stateVc = [[StatementViewController alloc] init];
    UINavigationController *stateNav = [[UINavigationController alloc] initWithRootViewController:stateVc];
    [self presentViewController:stateNav animated:YES completion:^{
        
    }];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

///// 手机号码的有效性判断
//检测是否是手机号码
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186，176
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[567]|7[67])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189
     22         */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
